package study.convertxml.entity;

import cn.hutool.core.util.XmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import study.convertxml.Base;
import study.convertxml.Item;
import study.convertxml.Report;
import study.convertxml.Xjxx;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class JavaToxmlEntity {
    public static void javaToxml(ReportZxjxcljg reportZxjxcljg) throws Exception {
        // 获取JAXB的上下文环境，需要传入具体的 Java bean -> 这里使用Student
        JAXBContext context = JAXBContext.newInstance(ReportZxjxcljg.class);
        StringWriter writer = new StringWriter();
        // 创建 Marshaller 实例
        Marshaller marshaller = context.createMarshaller();
        // 设置转换参数 -> 这里举例是告诉序列化器是否格式化输出
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // 构建输出环境 -> 这里使用标准输出，输出到控制台Console
        //PrintStream out = System.out;
        //marshaller.marshal(report, out);
        // 将所需对象序列化 -> 该方法没有返回值
        marshaller.marshal(reportZxjxcljg, writer);
        String xmlStr = writer.toString();
        xmlStr = StringUtils.replace(xmlStr, "&quot;", "'");
        System.out.println(xmlStr);
//        Document d = XmlUtil.beanToXml(reportZxjxcljg);
//        d.setXmlStandalone(true);
//        System.out.println(XmlUtil.format(d));
    }

    public static void main(String[] args) throws Exception {
        SendClxx sendClxx = new SendClxx(){{
            setCarNo("123");
            setArrStnName("456");
            setCarType("www");
            setPsSgmtName("www");
        }};
        SendClxx sendClxx1 = new SendClxx(){{
            setCarNo("ttt");
            setArrStnName("666");
            setCarType("sfds");
            setPsSgmtName("sdgsd");
        }};
        List<SendClxx> listClxx = new ArrayList<>();
        listClxx.add(sendClxx);
        listClxx.add(sendClxx1);
        ItemClxx itemClxx = new ItemClxx();
        itemClxx.setSendClxxList(listClxx);
        SendXjxx sendXjxx = new SendXjxx(){{
            setAcceptRep("ddd");
            setMaintType("ddd");
        }};
        SendZdc sendZdc = new SendZdc(){{
            setCarNo("jkdhfkj");
            setIhdlcarid("fkljhafkl");
            setShdlresultcode("flasf");
        }};
        SendZdc sendZdc1 = new SendZdc(){{
            setCarNo("ddddd");
            setIhdlcarid("fkljdsgsdfgsooooohafkl");
            setShdlresultcode("fl444444f");
        }};
        List<SendZdc> listZdc = new ArrayList<>();
        listZdc.add(sendZdc);
        listZdc.add(sendZdc1);
        ItemZdc itemZdc = new ItemZdc();
        itemZdc.setSendZdcList(listZdc);
        SendJzgz sendJzgz = new SendJzgz(){{
            setCarNo("jfjfjfjfjf");
            setJzgzCode("fjjjjj");
            setJzgzName("fffffffffff");
        }};
        SendJzgz sendJzgz1 = new SendJzgz(){{
            setCarNo("jfjfjf111111111111jfjf");
            setJzgzCode("fjj111111111111jjj");
            setJzgzName("ffffff111111111111fffff");
        }};
        List<SendJzgz> listJzgz = new ArrayList<>();
        listJzgz.add(sendJzgz);
        listJzgz.add(sendJzgz1);
        ItemJzgz itemJzgz = new ItemJzgz();
        itemJzgz.setSendJzgzList(listJzgz);
        Base base = new Base(){{
            setSjbs("1234");
            setCzbz("eee");
        }};
        ReportZxjxcljg report = new ReportZxjxcljg(){{
            setSendXjxx(sendXjxx);
           setItemClxx(itemClxx);
           setItemJzgz(itemJzgz);
           setItemZdc(itemZdc);
           setBase(base);
        }};

        javaToxml(report);
    }
}
